package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Planunit;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.PlanUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PlanUnitController {
    @Autowired
    PlanUnitService planUnitService;
    //查询订单项
    @GetMapping("/planunit")
    @ResponseBody
    public ResponseResult<List<Planunit>> getPlanUnit(Planunit planunit,int page,int limit){
        ResponseResult<List<Planunit>> responseResult = planUnitService.getPlanUnit(planunit,page,limit);
        return responseResult;
    }
    //添加订单项
    @ResponseBody
    @PostMapping("/planunit")
    public ResponseResult addPlanUnit(String prpId,String prdId,int pnumber){
        ResponseResult responseResult = planUnitService.addPlanUnit(prpId,prdId,pnumber);
        return responseResult;
    }
    //删除计划项
    @PostMapping("/deleteplanunits")
    @ResponseBody
    public ResponseResult deleteplanunits(@RequestParam("plaIds[]") int[] plaIds,String prpId){
        return planUnitService.deleteplanunits(plaIds,prpId);
    }
    //查询指定订单项
    @GetMapping("/planunitByid")
    @ResponseBody
    public ResponseResult<Map> getPlanUnitById(int plaId){
        return planUnitService.getPlanUnitById(plaId);
    }
    //修改订单项
    @ResponseBody
    @PostMapping("/updatePlanUnit")
    public ResponseResult updatePlanUnit(Planunit planunit,String prdId){
        try {
            return planUnitService.updatePlanUnit(planunit,prdId);

        }catch (BusinessException e){
            return new ResponseResult(1,e.getMessage(),null,null);
        }
    }
}
